package com.siruier.boss.ui.activity.user

import android.view.View
import com.siruier.boss.BuildConfig
import com.siruier.boss.api.core.launchUI
import com.siruier.boss.common.AGREEMENT
import com.siruier.boss.common.COMPANY_INTRODUCTION
import com.siruier.boss.common.PRIVACY_POLICY
import com.siruier.boss.databinding.ActivityAboutUsBinding
import com.siruier.boss.ui.activity.commom.openUrl
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.dialog.showAlertDialog
import com.siruier.boss.ui.ext.toastMessageLong
import com.siruier.boss.ui.helper.AppUpdateHelper
import com.siruier.boss.utils.GlideCatchUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class AboutUsActivity : BaseActivity<ActivityAboutUsBinding>() {
    override fun initView() {
        bindViewClick(vb.rlCompanyIntroduction, vb.rlVersionUpdate, vb.rlCleanCache, vb.rlUserAgreement, vb.rlPrivacyPolicy)
    }

    override fun initData() {
        vb.tvVersion.text = BuildConfig.VERSION_NAME
        launchUI({
            vb.tvCache.text = withContext(Dispatchers.IO) {
                GlideCatchUtil.getCacheSize()
            }
        })
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.rlCompanyIntroduction -> openUrl(COMPANY_INTRODUCTION)
            vb.rlVersionUpdate -> {
                AppUpdateHelper.checkUpdate(true) {
                    if (!it) {
                        "没有新版本".toastMessageLong()
                    }
                }
            }
            vb.rlCleanCache -> showAlertDialog {
                message(message = "确定清除缓存吗？")
                negativeButton { }
                positiveButton {
                    GlideCatchUtil.cleanCatchDisk()
                    vb.tvCache.text = "0KB"
                    "清除成功".toastMessageLong()
                }
            }
            vb.rlUserAgreement -> openUrl(AGREEMENT)
            vb.rlPrivacyPolicy -> openUrl(PRIVACY_POLICY)
        }
    }
}